
/**
 * Promise 构造函数接受
 * @param {[type]} executor [description]
 */
function Promise(executor) {
	var self = this

	// 状态和状态值
	this.promiseState = 'pending'
	this.promiseResult = undefined;
	// 存放成功/失败的回调
	this.callback = []

	function resolve(value) {
		if(self.promiseState !== 'pending')  return ;
		self.promiseState = 'fulfilled'
		self.promiseResult = value
		// console.log('我是成功的 resolve', arguments[1])
		setTimeout(() => {
			self.callback.forEach(({onResolve, onReject}) => {
				onResolve(self.promiseResult)
			})
		})
	}

	function reject(reason) {
		if(self.promiseState !== 'pending') return ;
		self.promiseState = 'rejected'
		self.promiseResult = reason

		// console.warn('我是失败的 reject', arguments[1])
		setTimeout(() => {
			self.callback.forEach(({onResolve, onReject}) => {
				onReject(self.promiseResult)
			})
		})
	}

	try{
		// 代码立即同步执行
		executor(resolve,reject)
	} catch(err) {
		reject(err)
	}

}

// then 的返回值 产生新的 Promise 【有缺陷】
Promise.prototype.then = function(onResolve, onReject) {
	function callback(onTypeFunc) {
		let ret ;
		try {
			ret = onTypeFunc(self.promiseResult)
		}catch(err) {
			reject(err)
		}
		if(ret instanceof Promise) {
			 return ret 
		} else {
			 return new Promise(resolve => {
			 	resolve(ret)
			 })
		}
	}

	if(this.promiseState === 'fulfilled') {
		// setTimeout(() => {
		return	callback(onResolve)
		// });
	}

	if(this.promiseState === 'rejected') {
		// setTimeout(() => {
		return callback(onReject)
		// });
	}

}

Promise.prototype.then1 = function(onResolve, onReject) {
	const self = this
	if(typeof onResolve !== 'function') {
		onResolve = value => value
	}

	if(typeof onReject !== 'function') {
		onReject = reason => {
			throw reason
		}
	}
	return new Promise((resolve, reject) => {

			function callback(onTypeFunc) {
				let ret ;
				try {
					ret = onTypeFunc(self.promiseResult)
				}catch(err) {
					reject(err)
				}

				if(ret instanceof Promise) {
					 ret.then(v=>{
					 	resolve(v)
					 }, r=>{
					 	reject(r)
					 })
				} else {
					 resolve(ret)
				}
			}

			if(this.promiseState === 'fulfilled') {
				setTimeout(() => {
					callback(onResolve)
				});
			}

			if(this.promiseState === 'rejected') {
				setTimeout(() => {
					callback(onReject)
				});
			}

			if(this.promiseState === 'pending') {
				this.callback.push({
					onResolve: function() {
						callback(onResolve)
					},
					onReject: function() {
						callback(onReject)
					}
				})
			}
		})

}


// Promise.prototype.catch()方法是.then(null, rejection)或.then(undefined, rejection)的别名，
// 用于指定发生错误时的回调函数
Promise.prototype.catch = function(onRejected) {
	return this.then(null, onRejected)
}

//添加 resolve 方法, 返回 promise 对象
Promise.resolve = function(value) {
	if(value instanceof Promise) {
		return value
	} else {
		return new Promise(resolve => {
			resolve(value);
		})
	}
}

//添加 resolve 方法, 返回 有一个失败的promise 对象
Promise.reject = function(reason) {
	return new Promise((resolve, reject) => {
		if(reason instanceof Promise) {
			reason.then(v => {
				reject(v)
			}, r => {
				reject(r)
			})
		} else {
			reject(reason)
		}
	})
}













if(!(typeof window === 'object' && this === window)) {
	//实例化对象
	let p = new Promise((resolve, reject) => {
		setTimeout(() => {
			// reject('ERROR');
			resolve('OK');
		}, 1000);
	});


	if(1) {
		console.log(p.then())
		console.log('dddddd')
	} else {

		//值传递
		p.then()
			.then(value=>{
				console.log(222, value);
			})
			.then(value => {
				console.log(333, value);
			})
		// .catch(reason => {
		//     console.warn(reason);
		// });
	}
	console.log(p)
}
